package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.entity.SpuSaleAttrValue;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.atguigu.gmall.product.vo.SpuSaveInfoVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import io.minio.messages.Item;
import jodd.bean.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 *
 */
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
    implements SpuInfoService{

    @Autowired
    SpuImageService spuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    @Autowired
    SpuSaleAttrValueService spuSaleAttrValueService;

    @Override
    public void saveSpuInfo(SpuSaveInfoVo spuSaveInfoVo) {
        //1.存spu_info
        SpuInfo spuInfo = new SpuInfo();
        BeanUtils.copyProperties(spuSaveInfoVo, spuInfo);
        save(spuInfo);
        //获取spuid
        Long spuId = spuInfo.getId();

        //2.存图片
        //获取前端图片集合
        List<SpuSaveInfoVo.SpuImageListDTO> imageList = spuSaveInfoVo.getSpuImageList();
        //创建图片数据库集合
        List<SpuImage> spuImageList = new ArrayList<>();
        //遍历前端数据，存入数据库集合
//        for (SpuSaveInfoVo.SpuImageListDTO spuImageListDTO : imageList) {
//            //对应数据库对象
//            SpuImage spuImage = new SpuImage();
//            //复制数据
//            BeanUtils.copyProperties(spuImageListDTO, spuImage);
//            //回填id
//            spuImage.setSpuId(spuId);
//            spuImageList.add(spuImage);
//        }
        //使用stream
        spuImageList = imageList.stream().map(item -> {
            //对应数据库对象
            SpuImage spuImage = new SpuImage();
            //复制数据
            BeanUtils.copyProperties(item , spuImage);
            //回填id
            spuImage.setSpuId(spuId);
            return spuImage;
        }).collect(Collectors.toList());
        spuImageService.saveBatch(spuImageList);

        //3.存属性名 spuSaleAttr
        List<SpuSaveInfoVo.SpuSaleAttrListDTO> spuSaleAttrList = spuSaveInfoVo.getSpuSaleAttrList();
        List<SpuSaleAttr> saleAttrList = spuSaleAttrList.stream().map(item -> {
            SpuSaleAttr spuSaleAttr = new SpuSaleAttr();
            BeanUtils.copyProperties(item, spuSaleAttr);
            spuSaleAttr.setSpuId(spuId);
            return spuSaleAttr;
        }).collect(Collectors.toList());
        spuSaleAttrService.saveBatch(saleAttrList);

        //4.存属性值
        List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttrList.stream().flatMap(item -> {
            Stream<SpuSaleAttrValue> spuSaleAttrValueStream = item.getSpuSaleAttrValueList().stream().map(itemValue -> {
                SpuSaleAttrValue spuSaleAttrValue = new SpuSaleAttrValue();
                spuSaleAttrValue.setSpuId(spuId);
                spuSaleAttrValue.setBaseSaleAttrId(itemValue.getBaseSaleAttrId());
                spuSaleAttrValue.setSaleAttrValueName(itemValue.getSaleAttrValueName());
                spuSaleAttrValue.setSaleAttrName(item.getSaleAttrName());
                return spuSaleAttrValue;
            });
            return spuSaleAttrValueStream;
        }).collect(Collectors.toList());
        spuSaleAttrValueService.saveBatch(spuSaleAttrValueList);
    }
}




